package com.party.mobile.biz.order.payOrder;

import com.party.common.utils.BigDecimalUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PartnerMember;
import com.party.core.model.order.OrderForm;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import com.party.mobile.biz.activity.ActivityBizService;
import com.party.mobile.biz.analyze.MemberMerchantDetailBizService;
import com.party.notify.notifyPush.officialPartner.IOfficialPartnerNotifySendService;
import com.party.notify.notifyPush.servce.INotifySendService;
import com.party.pay.service.pay.PayOrderBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ActivityPayOrderService
 * @Description 活动订单回调处理
 * @Author wei.li
 * @Date 2019/4/17
 **/
@Service
@Component("activityPayOrderService")
public class ActivityPayOrderService extends IPayOrderService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private IActivityService activityService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private INotifySendService notifySendService;
    @Autowired
    private PayOrderBizService payOrderBizService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private IOfficialPartnerNotifySendService officialPartnerNotifySendService;
    @Autowired
    private MemberMerchantDetailBizService memberMerchantDetailBizService;
    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;
    @Autowired
    private ActivityBizService activityBizService;
    @Autowired
    private IMemberGroupService memberGroupService;

    @Override
    public Object doMain(OrderForm orderForm) {
        // 修改活动报名状态
        activityBizService.updateMemberAct(orderForm.getId());
        // 更新活动数据
        this.updateActivity(orderForm.getGoodsId(), orderForm.getPayment());
        return null;
    }

    @Override
    public void doAssist(OrderForm orderForm, Object resultObject) {
        Activity activity = activityService.get(orderForm.getGoodsId());
        try {
            // 活动支付成功消息推送
            sendActivityMessage(orderForm, activity);
        } catch (Exception e) {
            logger.error("活动消息推送异常", e);
        }

        try {
            // 合作商统计数
            memberMerchantDetailBizService.updateActivityPaySuccess(orderForm.getInitiatorId(), orderForm.getPayment());
        } catch (Exception e) {
            logger.error("实时更新合作商统计数异常", e);
        }

        try {
            // 个人统计数
            PartnerMember partnerMember = partnerMemberService.findByPartnerAndMember(orderForm.getInitiatorId(), orderForm.getMemberId());
            memberPersonalCountBizService.countOne(partnerMember);
        } catch (Exception e) {
            logger.error("实时更新个人统计数异常", e);
        }
    }

    /**
     * @param activityId 活动id
     * @param payment    金额
     * @return
     * @Author yifeng
     * @Description 更新活动数据
     * @Date 14:10 2019/4/11
     **/
    public Activity updateActivity(String activityId, Float payment) {
        Activity activity = activityService.get(activityId);

        activity.setOrderNum(activity.getOrderNum() + 1);

        Double totalMoney = BigDecimalUtils.add(activity.getTotalMoney(), payment);
        totalMoney = BigDecimalUtils.round(totalMoney, 2);
        activity.setTotalMoney(totalMoney);

        activity.setSalesNum(activity.getSalesNum() + 1);

        Float salesAmount = BigDecimalUtils.add(activity.getSalesAmount(), payment);
        salesAmount = BigDecimalUtils.round(salesAmount, 2);
        activity.setSalesAmount(salesAmount);

        activityService.update(activity);
        return activity;
    }

    /**
     * @param orderForm 订单信息
     * @return
     * @Author yifeng
     * @Description 报名支付成功消息推送
     * @Date 14:56 2019/4/10
     **/
    public void sendActivityMessage(OrderForm orderForm, Activity activity) {
        Member buyerMember = memberService.get(orderForm.getMemberId());
        MemberGroup orgMember = memberGroupService.get(orderForm.getInitiatorId());
        List<String> verifyCodeList = payOrderBizService.getCouponsList(orderForm);
        for (String verifyCode : verifyCodeList) {
            try {
                notifySendService.sendActivityApply(orderForm, verifyCode, activity, orgMember);
            } catch (Exception e) {
                logger.error("活动报名成功---通知报名者", e);
            }
            try {
                officialPartnerNotifySendService.sendActivityApply(buyerMember.getRealname(), orderForm.getTitle(),
                        orderForm.getGoodsId(), orgMember.getMobile(), orgMember.getId());
            } catch (Exception e) {
                logger.error("活动报名成功---通知合作商", e);
            }
        }
    }
}
